package com.briup.day16;

/**
 * @author ThinkBook
 */
public class Test03_Object {
    public static void main(String[] args) {
        Stu s1 = new Stu();
        Stu s2 = new Stu();
        System.out.println(s1.hashCode() == s2.hashCode());
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
    public static void main01(String[] args) {
        //1.两个相同的对象，其hashCode值肯定相同
        Integer i1 = 127;
        Integer i2 = 127;

        System.out.println("i1.hashCode:" + i1.hashCode());
        System.out.println("i2.hashCode:" + i2.hashCode());

        //2.俩个引用变量的hashCode值不同，则它们肯定不可能指向同一个对象
        Integer i3 = new Integer(128);
        System.out.println("i3.hashCode:" + i3.hashCode());

        //3.两个对象的hashCode值相同，则这两个对象 可能一样，也可能不一样
        String s1 = "NB";
        String s2 = "Ma";
        System.out.println("NB.hashCode: " + s1.hashCode());
        System.out.println("Ma.hashCode: " + s2.hashCode());
    }
}
class Stu{
    private  String name;
    private int age;

    public Stu(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Stu() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Stu stu = (Stu) o;

        if (age != stu.age) return false;
        return name != null ? name.equals(stu.name) : stu.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }
}
